package com.atguigu.szdz.dcsyl.controller;

import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.service.SysService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/sys")
public class SysController {
    @Autowired
    SysService sysService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("/updateMune")
    public R updateMune(@RequestBody Map<String, String> params) {
        String sort = params.get("sort");

        Integer integer = 0;
        if (StringUtils.equals(sort, "add")) {
            integer = sysService.addMune(params);
        } else if (StringUtils.equals(sort, "edit")) {
            integer = sysService.editMune(params);
        } else if (StringUtils.equals(sort, "delete")) {
            String id = params.get("id");
            System.out.println("ididididididididid:" + id);
            integer = sysService.deleteMune(id);
        }
//        integer=0;

        return R.ok().put("integer", integer);
    }

    @RequestMapping("/getMuneTree")
    public R getMuneTree() {
        List<Map<String, Object>> muneTreeList = sysService.getMuneTree();

        return R.ok().put("muneTreeList", muneTreeList);
    }

    @RequestMapping("/deleteToken")
    public R deleteToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        Boolean aBoolean = redisTemplate.delete(token);

        return R.ok().put("aBoolean", aBoolean);
    }

    @RequestMapping("/getUserName")
    public R getUserName(HttpServletRequest request) {
        String userId = request.getHeader("userId");

        String userName = (String) redisTemplate.opsForHash().get(userId, "userName");

        return R.ok().put("userName", userName);
    }

    @RequestMapping("/login")
    public R login(@RequestBody Map<String, String> params) {
        String userName = params.get("userName");
        String pass = params.get("pass");

        List<Map<String, Object>> userList = sysService.selectUserByUserName(userName);

        if (userList.size() <= 0 || userList == null) {
            return R.error().put("result", "fail").put("message", "用户不存在!");
        }

        String salt = (String) userList.get(0).get("salt");
        String finalPassword = (String) userList.get(0).get("password");
        String finalPassword2 = DigestUtils.md5DigestAsHex((pass + salt).getBytes());
        String userId = String.valueOf(userList.get(0).get("id"));
        System.out.println("uuuuuuuuuuuuuu:" + userId);

        if (!StringUtils.equals(finalPassword, finalPassword2)) {
            return R.error().put("result", "fail").put("message", "密码不正确!");
        }

        String token = "Bearer" + UUID.randomUUID().toString().substring(0, 11);
        String mobile = (String) userList.get(0).get("mobile");

        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("userName", userName);
        hashMap.put("mobile", mobile);
        hashMap.put("token", token);

        redisTemplate.delete("auth-" + userId);
        redisTemplate.opsForHash().putAll("auth-" + userId, hashMap);

        Map<String, Object> map = new HashMap<>();
        map.put("userId", "auth-" + userId);
        map.put("token", token);
        return R.ok().put("result", "success").put("loginMap", map);
    }

    @RequestMapping("/register")
    public R register(@RequestParam Map<String, String> params) {
        String userName = params.get("userName");
        String pass = params.get("pass");

        List<Map<String, Object>> userList = sysService.selectUserByUserName(userName);

        if (userList.size() > 0 && userList != null) {
            return R.error("该用户名已经注册!");
        }

        //生成随机盐值
        String salt = UUID.randomUUID().toString().replace("-", "");
        String finalPassword = DigestUtils.md5DigestAsHex((pass + salt).getBytes());

        params.put("salt", salt);
        params.put("finalPassword", finalPassword);

        sysService.add(params);

        return R.ok();
    }

}
